<?php

namespace admin\controllers;

use ijony\helpers\File;
use ijony\helpers\Folder;
use Yii;
use admin\models\Products;
use yii\data\ActiveDataProvider;
use yii\filters\AccessControl;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;

/**
 * ProductController implements the CRUD actions for Products model.
 */
class ProductController extends BaseController
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs'  => [
                'class'   => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Products models.
     * @return mixed
     */
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => Products::find(),
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Products model.
     *
     * @param string $id
     *
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Products model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Products();
        $model->status = Products::STATUS_ACTIVE;

        if($model->load(Yii::$app->request->post())){
            $preview = UploadedFile::getInstance($model, 'preview');
            if($preview){
                $model->preview = File::newFile($preview->getExtension());
            }

            if(is_array($model->veneer)){
                $model->veneer = join(",", $model->veneer);
            }

            if(is_array($model->outside_handle)){
                $model->outside_handle = join(",", $model->outside_handle);
            }

            if(is_array($model->inside_handle)){
                $model->inside_handle = join(",", $model->inside_handle);
            }

            if(is_array($model->lockset)){
                $model->lockset = join(",", $model->lockset);
            }

            if($model->save()){
                if($preview){
                    $preview->saveAs(Folder::getStatic($model->preview));
                }

                return $this->redirect(['view', 'id' => $model->id]);
            }
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing Products model.
     * If update is successful, the browser will be redirected to the 'view' page.
     *
     * @param string $id
     *
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if($model->load(Yii::$app->request->post())){
            $preview = UploadedFile::getInstance($model, 'preview');
            if($preview){
                $model->preview = File::newFile($preview->getExtension());
            }

            if(is_array($model->veneer)){
                $model->veneer = join(",", $model->veneer);
            }

            if(is_array($model->outside_handle)){
                $model->outside_handle = join(",", $model->outside_handle);
            }

            if(is_array($model->inside_handle)){
                $model->inside_handle = join(",", $model->inside_handle);
            }

            if(is_array($model->lockset)){
                $model->lockset = join(",", $model->lockset);
            }

            if($model->save()){
                if($preview){
                    $preview->saveAs(Folder::getStatic($model->preview));
                }

                return $this->redirect(['view', 'id' => $model->id]);
            }
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    /**
     * Deletes an existing Products model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     *
     * @param $id
     *
     * @return \yii\web\Response
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Products model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     *
     * @param string $id
     *
     * @return Products the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if(($model = Products::findOne($id)) !== NULL){
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
